﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Qzone.OAuth.WebControls
{
    [ToolboxData("<{0}:LoginButton runat=\"server\"></{0}:LoginButton>")]
    [ToolboxBitmap(typeof(LoginButton), "LoginButton.bmp")]
    [DefaultEvent("Logined")]
    [DefaultProperty("ImageUrl")]
    public class LoginButton : System.Web.UI.WebControls.Image, IPostBackEventHandler
    {
        internal static readonly object EventOnLogined = new object();

        [Category("Qzone.OAuth")]
        [Description("登录动作完成后，调用的客户端脚本方法。写法如：alert(1);")]
        [Browsable(true)]
        public string OnClientLogined
        {
            get
            {
                object o = this.ViewState["OnClientLogined"];
                return o != null ? (string)o : string.Empty;
            }

            set
            {
                this.ViewState["OnClientLogined"] = value;
            }
        }

        [Category("Qzone.OAuth")]
        [Description("登录动作完成后，回调的服务器事件。")]
        public event EventHandler Logined
        {
            add
            {
                this.Events.AddHandler(LoginButton.EventOnLogined, value);
            }
            remove
            {
                this.Events.RemoveHandler(LoginButton.EventOnLogined, value);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.DesignMode)
            {
                return;
            }

            this.Page.ClientScript.GetPostBackEventReference(this, string.Empty);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if (this.DesignMode)
            {
                return;
            }

            EventHandler headler = (EventHandler)this.Events[LoginButton.EventOnLogined];

            if (headler != null)
            {
                headler(this, EventArgs.Empty);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.DesignMode)
            {
                return;
            }

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Qzone.OAuth.WebControls.LoginButton.js", @"
function Open_Qzone_OAuth_Window(btnid, clientfun, ispostback) 
{
    window.open(""qzoneoauth.axd?type=loginbutton&btnid="" + btnid + ""&clientfun="" + clientfun + ""&ispostback="" + ispostback + ""&R="" + Math.random(), null, ""width=450,height=320,menubar=0,scrollbars=0,resizable=1,status=0,titlebar=0,toolbar=0,location=1"");
}

function Close_Qzone_OAuth_Window(btnid, clientfun, ispostback)
{
            try { 
            eval(clientfun);
        } catch (e) {        }

            if (ispostback == ""true"") {
                window.__doPostBack(btnid, """");
            }
}

", true);
            string ispostback = "false";
            EventHandler headler = (EventHandler)this.Events[LoginButton.EventOnLogined];

            if (headler != null)
            {
                ispostback = "true";
            }

            this.Attributes.Add("onclick", string.Format("Open_Qzone_OAuth_Window(\"{0}\",\"{1}\",\"{2}\");", this.UniqueID, this.OnClientLogined, ispostback));
        }
    }
}
